package xyz.xxin.xat.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.location.Location;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.amap.api.location.AMapLocation;

import xyz.xxin.xat.R;
import xyz.xxin.xat.utils.LocationUtils;
import xyz.xxin.xat.utils.CoordinateUtils;
import xyz.xxin.xat.utils.PermissionUtils;

public class GaoDeActivity extends AppCompatActivity {

    private TextView longitude_text;
    private TextView latitude_text;
    private TextView longitude_gd_text;
    private TextView latitude_gd_text;
    private Button again_get_location;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gao_de);
        request();
        findView();
        initLocation();
        addListener();
    }

    @SuppressLint("SetTextI18n")
    private void addListener() {
        AMapLocation aMapLocation = LocationUtils.getInstance().getLocation();
        again_get_location.setOnClickListener(view -> {
            if (aMapLocation != null){
                longitude_gd_text.setText("高德经度:" + aMapLocation.getLongitude());
                latitude_gd_text.setText("高德维度:" + aMapLocation.getLatitude());

                Location location = CoordinateUtils.GCJ02ToWGS84(aMapLocation.getLatitude(), aMapLocation.getLongitude());
                longitude_text.setText("经度:" + location.getLongitude());
                latitude_text.setText("维度:" + location.getLatitude());
            } else {
                Toast.makeText(this, "获取不到位置信息", Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void initLocation() {
        LocationUtils.getInstance().init(this);
    }

    private void findView() {
        longitude_text = findViewById(R.id.longitude_text);
        latitude_text = findViewById(R.id.latitude_text);
        longitude_gd_text = findViewById(R.id.longitude_gd_text);
        latitude_gd_text = findViewById(R.id.latitude_gd_text);
        again_get_location = findViewById(R.id.again_get_location);
    }

    /**
     * 请求权限
     */
    private void request() {
        if (!PermissionUtils.isWrite(this)){
            PermissionUtils.requestWrite(this);
        }
        else if (!PermissionUtils.isCoarseLocation(this)){
            PermissionUtils.requestCoarseLocation(this);
        }
        else if (!PermissionUtils.isFineLocation(this)){
            PermissionUtils.requestFineLocation(this);
        }
        else if (!PermissionUtils.isReadPhoneState(this)){
            PermissionUtils.requestReadPhoneState(this);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        request();
    }
}